function getConsumers(){
    var api = "/backstage/users/consumers";
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200) {
                var costumers = data.data;
                $("#consumerDataTable").DataTable({
                    data: costumers,
                    columnDefs: [
                        {
                            "targets": "consumerID",
                            "data": "uid",
                        },
                        {
                            "targets": "consumerNickname",
                            "data": "nickname",
                        },
                        {
                            "targets": "consumerTotalCost",
                            "data": "total_cost",
                        },
                        {
                            "targets": "consumerEmail",
                            "data": "email",
                        },
                        {
                            "targets": "consumerPhone",
                            "data": "phone",
                        },
                        {
                            "targets": "consumerTranscriptNum",
                            "data": "transcript_num",
                        },
                        {
                            "targets": "consumerPrintNum",
                            "data": "print_num",
                        },
                        {
                            "targets": "consumerJoinTime",
                            "data": "join_time",
                        },
                        {
                            "targets": "consumerOperation",
                            "defaultContent": 
                            '<button class="btn btn-primary btn-xs consumerDetail">查看</button>' +
                            '<button class="btn btn-primary btn-xs consumerAddress">地址</button>' +
                            '<button class="btn btn-primary btn-xs consumerCost">消费</button>',
                        },
                    ],
                });
            }
            else {
                alert("请求异常，请刷新");
            }
        },
    })
}

function getAddressList(userID) {
    var api = "/backstage/users/consumers/" + userID + "/address";
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200){
                var address_list = data.data;
                console.log(address_list)
                $("#addressDataTable").DataTable({
                    data: address_list,
                    columnDefs: [
                        {
                            "targets": "contact",
                            "data": "contact",
                        },
                        {
                            "targets": "phone",
                            "data": "phone",
                        },
                        {
                            "targets": "postcode",
                            "data": "postcode",
                        },
                        {
                            "targets": "address",
                            "data": "address",
                        },
                    ],
                });
            }
            else {
                alert("请求异常，请刷新");
            }
        },
    });
}

function getConsumerFavoriteList(){
    var api = "/backstage/users/consumers/favorite";
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200){
                var favoriteList = data.data;
                $("#favoriteDataTable").DataTable({
                    data: favoriteList,
                    columnDefs: [
                        {
                            "targets": "consumerID",
                            "data": "uid",
                        },
                        {
                            "targets": "consumerNickname",
                            "data": "nickname",
                        },
                        {
                            "targets": "sutraEditionName",
                            "data": "sutra_edition_name",
                        },
                        {
                            "targets": "favoriteTime",
                            "data": "create_time",
                        },
                        {
                            "targets": "favoriteNum",
                            "data": "favorite_num",
                        },
                        {
                            "targets": "transcriptStatus",
                            "data": "transcript_status",
                        },
                    ],
                });
            }
            else {
                alert("请求异常，请刷新");
            }
        },
    });
}

function getConsumerTranscriptList(){
    var api = "/backstage/users/consumers/transcription";
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200){
                var transcriptList = data.data;
                $("#transcriptDataTable").DataTable({
                    data: transcriptList,
                    columnDefs: [
                        {
                            "targets": "consumerID",
                            "data": "uid",
                        },
                        {
                            "targets": "consumerNickname",
                            "data": "nickname",
                        },
                        {
                            "targets": "sutraEditionName",
                            "data": "sutra_edition_name",
                        },
                        {
                            "targets": "finishTime",
                            "data": "finish_time",
                        },
                        {
                            "targets": "selfNum",
                            "data": "self_num",
                        },
                        {
                            "targets": "assistanceNum",
                            "data": "assistance_num",
                        },
                        {
                            "targets": "printStatus",
                            "data": "print_status",
                        },
                    ],
                });
            }
            else {
                alert("请求异常，请刷新");
            }
        },
    });
}

function getConsumerAssistanceList(){
    var api = "/backstage/users/consumers/assistance";
    $.ajax({
        url: api,
        type: "GET",
        success: function(data, status){
            if(data.status == 200){
                var assistanceList = data.data;
                $("#assistanceDataTable").DataTable({
                    data: assistanceList,
                    columnDefs: [
                        {
                            "targets": "consumerID",
                            "data": "uid",
                        },
                        {
                            "targets": "consumerNickname",
                            "data": "nickname",
                        },
                        {
                            "targets": "sutraEditionName",
                            "data": "sutra_edition_name",
                        },
                        {
                            "targets": "finishTime",
                            "data": "finish_time",
                        },
                        {
                            "targets": "pageNum",
                            "data": "page_num",
                        },
                        {
                            "targets": "transcriptStatus",
                            "data": "print_status",
                        },
                    ],
                });
            }
            else {
                alert("请求异常，请刷新");
            }
        },
    });
}

// function getConsumerDetail(userID) {
//     var api = "/backstage/consumers/users/" + userID;
//     $.ajax({
//         url: api,
//         type: "GET",
//         success: function(data, status){
//             if(data.status == 200){
//                 console.log(data.data);
//             }
//             else {
//                 alert("请求异常，请刷新");
//             }
//         },
//     });

// }

function setHotWords(hotwordsStr){
    var api = "/backstage/system/hotwords";
    var data = {
        "words": hotwordsStr,
    };
    var csrftoken = getCookie('csrftoken');
    $.ajax({
        url: api,
        type: "POST",
        data: data,
        beforeSend: function(xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        },
        success: function(data, status){
            if(data.status == 200) {
                console.log("请求成功");
            }
            else {
                alert("请求异常，请刷新");
            }
        }
    })
}

function initConsumerList(){
    getConsumers();
    $('#consumerDataTable').on('click', 'button', function () {
        var table = $("#consumerDataTable").DataTable();
        var userID = table.row($(this).parents('tr')).data().id;
        var actionName = $(this)[0].classList[3];

        var actionMap = {
            "consumerDetail": "consumer_detail.html",
            "consumerAddress": "consumer_address_list.html",
            "consumerCost": "consumer_cost_list.html",
        }

        var href = actionMap[actionName] + "?userID=" + userID;

        window.location.href = href;
    } );
}

function initAddressList() {
    var userID = getQueryString("userID");
    getAddressList(userID);
}

function initConsumerDetail() {
    var userID = getQueryString("userID");
    getConsumerDetail(userID);
}

function initConsumerFavoriteList() {
    getConsumerFavoriteList();

}

function initConsumerTranscriptList() {
    getConsumerTranscriptList();
}

function initConsumerAssistanceList() {
    getConsumerAssistanceList();
}

function init() {
    var parts = window.location.pathname.split("/");
    var lastPart = parts[parts.length - 1];

    var initFuncMap = {
        "consumer_list.html": initConsumerList,
        "consumer_address_list.html": initAddressList,
        "consumer_detail.html": initConsumerDetail,
        "consumer_favorite_list.html": initConsumerFavoriteList,
        "consumer_transcript_list.html": initConsumerTranscriptList,
        "consumer_assistance_list.html": initConsumerAssistanceList,
    };

    var initFunc = initFuncMap[lastPart];
    initFunc();
}

$(function(){
    init();
})
